package org.linlinjava.litemall.admin.web;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.admin.util.CreateSns;
import org.linlinjava.litemall.core.systemConfig.GetRegionService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallAddressService;
import org.linlinjava.litemall.db.service.LitemallAwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @author zhy
 * @date 2019-07-02 18:09
 **/
@RestController
@RequestMapping("/admin/award")
public class AdminAwardController extends GetRegionService {
	@Autowired
	private LitemallAwardService litemallAwardService;
	@Autowired
	private LitemallAddressService addressService;
	@Resource
	private CreateSns createSns;
	/**
	 * 获取将奖品列表
	 *
	 * @param userId
	 * @param awardName
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	public Object getList(
		@LoginAdmin Integer userId, String awardName, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		List<LitemallAward> litemallAwards = litemallAwardService.getList(awardName, page, limit);
		long total = PageInfo.of(litemallAwards).getTotal();
		Map<String, Object> data = new HashMap<>(2);
		data.put("total", total);
		data.put("items", litemallAwards);
		return ResponseUtil.ok(data);
	}

	/**
	 * 获取抽奖记录
	 * @param userId
	 * @param awardName
	 * @param page
	 * @param limit
	 * @param sendFlag
	 * @param start
	 * @param end
	 * @return
	 */
	@GetMapping("/lotteryList")
	public Object lotteryList(
		@LoginAdmin Integer userId,
		String awardName,
		@RequestParam(value = "page", defaultValue = "1") Integer page,
		@RequestParam(value = "limit", defaultValue = "10") Integer limit,
		@RequestParam(value = "sendFlag", defaultValue = "4") short sendFlag,
		String start,String end) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		LocalDate starts = null;
		LocalDate ends = null;
		//解析时间
		if (StringUtils.isNotEmpty(start)&& StringUtils.isNotEmpty(end)) {
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			starts = LocalDate.parse(start, df);
			ends = LocalDate.parse(end, df);
		}
		List<LitemallUserLottery> litemallAwards = litemallAwardService.getlotteryList(starts,ends, page, limit,sendFlag);
		long total = PageInfo.of(litemallAwards).getTotal();
		Map<String, Object> data = new HashMap<>(2);
		data.put("total", total);
		data.put("items", litemallAwards);
		return ResponseUtil.ok(data);
	}

	/**
	 * 抽奖用户发货发货
	 * @param userId
	 * @param id
	 * @return
	 */
	@GetMapping("/doSendAward")
	public Object doSendAward(@LoginAdmin Integer userId, Integer id){
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		LitemallUserLottery litemallUserLottery = new LitemallUserLottery();
		litemallUserLottery.setId(id);
		litemallUserLottery.setSendFlag((short)2);
		litemallAwardService.updeteSendFlag(litemallUserLottery);
		return ResponseUtil.ok();
	}

	@GetMapping("/getAddress")
	public Object getAddress(@LoginAdmin Integer userId,Integer id){
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		//找到用户的默认地址
		LitemallAddress litemallAddress = addressService.findDefault(id,false);
		if (litemallAddress==null){
			return ResponseUtil.fail(-1,"没有收货地址");
		}
		Map<String,Object> map = new HashMap<>(4);
		map.put("litemallAddress",litemallAddress);
		map.put("detailAddress",getdetailedAddress(litemallAddress));
		map.put("createSns",createSns.createSn());
		return ResponseUtil.ok(map);
	}

	/**
	 * 根据奖品Id获取活动计划表
	 *
	 * @param userId
	 * @param awardId
	 * @return
	 */
	@GetMapping("/getPlan")
	public Object getPlan(
		@LoginAdmin Integer userId,@NotNull Integer awardId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		List<LitemallAwardPlan> awardPlanList = litemallAwardService.getPlanByAwardId(awardId);
		long total = PageInfo.of(awardPlanList).getTotal();
		Map<String, Object> data = new HashMap<>(2);
		data.put("total", total);
		data.put("items", awardPlanList);
		return ResponseUtil.ok(data);
	}

	/**
	 * 删除计划
	 *
	 * @param userId
	 * @param planId
	 * @return
	 */
	@GetMapping("deletePlan")
	public Object deletePlan(
		@LoginAdmin Integer userId, Integer planId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		litemallAwardService.deletePlanByAwardId(planId);
		return ResponseUtil.ok();
	}

	/**
	 * 综合统计
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/statistics")
	public Object getStatic(@LoginAdmin Integer userId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		// 查询今日奖池份额
		FutureTask<Integer> toDayAwardFutureTask = new FutureTask<>(()-> litemallAwardService.getTodayAwardPrice());
		// 查询今日用户抽奖消耗
		FutureTask<Integer> toDayUseFutureTask = new FutureTask<>(()-> litemallAwardService.getTodayUse());
		//查询最近3天的抽奖消耗
		FutureTask<Integer> threeDaysUseFutureTask = new FutureTask<>(()-> litemallAwardService.three());
		// 查询今日奖池抽中比例
		FutureTask<Integer> allNumFutureTask = new FutureTask<>(()-> litemallAwardService.allNum());
		FutureTask<Integer> leftNumFutureTask = new FutureTask<>(()-> litemallAwardService.leftNum());

		executorService.submit(toDayAwardFutureTask);
		executorService.submit(toDayUseFutureTask);
		executorService.submit(threeDaysUseFutureTask);
		executorService.submit(allNumFutureTask);
		executorService.submit(leftNumFutureTask);

		Map<String,Object> map = new HashMap(8);

		try {
			map.put("todayAwardPrice",toDayAwardFutureTask.get());
			map.put("todayUse",toDayUseFutureTask.get());
			map.put("threeDaysUse",threeDaysUseFutureTask.get());
			Integer allNum = allNumFutureTask.get();
			Integer leftNum = leftNumFutureTask.get();
			map.put("percent",(allNum-leftNum)+"/"+allNum);
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
		catch (ExecutionException e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok(map);
	}

	/**
	 * 创建奖品信息
	 *
	 * @param userId
	 * @param litemallAward
	 * @return
	 */
	@PostMapping("/create")
	public Object create(
		@LoginAdmin Integer userId, @RequestBody LitemallAward litemallAward) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		litemallAwardService.addAward(litemallAward);
		return ResponseUtil.ok(litemallAward);
	}

	/**
	 * 创建奖品活动日期
	 *
	 * @param userId
	 * @param litemallAwardPlan
	 * @return
	 */
	@PostMapping("/createPlan")
	public Object createPlan(
		@LoginAdmin Integer userId, @RequestBody LitemallAwardPlan litemallAwardPlan) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		litemallAwardPlan.setLeftNum(litemallAwardPlan.getNum());
		litemallAwardService.addPlan(litemallAwardPlan);
		return ResponseUtil.ok(litemallAwardPlan);
	}


	/**
	 * 更新奖品信息
	 *
	 * @param userId
	 * @param litemallAward
	 * @return
	 */
	@PostMapping("/update")
	public Object update(
		@LoginAdmin Integer userId, @RequestBody LitemallAward litemallAward) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		litemallAwardService.upAward(litemallAward);
		return ResponseUtil.ok(litemallAward);
	}

	/**
	 * 获取此将奖品的获奖人员
	 *
	 * @param userId
	 * @param awardId
	 * @return
	 */
	@GetMapping("/getAwardUser")
	public Object getAwardUser(@LoginAdmin Integer userId, Integer awardId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		return ResponseUtil.ok(litemallAwardService.getUserById(awardId));
	}
}
